import { Inject, Provide } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { NovelEntity } from 'src/entity/novel/novel';
import { NovelPartEntity } from 'src/entity/part/part';
import { Repository  } from "typeorm";
import { Context } from '@midwayjs/koa';
import * as jwt from 'jsonwebtoken';

@Provide()
export class NovelService {

    @Inject()
    ctx: Context

    @InjectEntityModel(NovelEntity)
    NovelModel: Repository<NovelEntity>;
    @InjectEntityModel(NovelPartEntity)
    NovelPartModel: Repository<NovelPartEntity>;
 

  async getNovel(options:any ) {
    let Novel = await this.NovelModel.createQueryBuilder('novel')
    .where("novel.id = :id", { id: 1 })
    .leftJoinAndSelect('novel.novels','item')
    .select(['novel'])
    .addSelect(['item.articleTitle'])
    .getOne()
    return Novel
  }

  
  async getNovelitem(options:any ) {
    let Novel = await this.NovelPartModel.createQueryBuilder('novel')
    .where("novel.id = :id", { id: 1 })
    .getOne()

    if(Novel.status){
            // 查下用户是否购买 , 需要获取token 查找id 查找订单
            
    }
    return Novel
  }


  async getuserinfo(){
    let result = await  jwt.verify(this.ctx.get("Authorization"),'asdasd')
    console.log("解析", typeof result == 'object');
    if( typeof result == 'object'){
      return result
    }
  }



}
